/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.rmc.binding;

import org.vacoor.nothing.misc.rmc.session.Session;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 代理工厂
 *
 * @author vacoor
 */
public class MapperProxyFactory<I> {
    private Map<Method, MapperMethod<I>> methodCache = new ConcurrentHashMap<Method, MapperMethod<I>>();
    private Class<I> mapperInterface;

    public MapperProxyFactory(Class<I> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public I newProxy(Session session) {
        MapperProxy<I> proxy = new MapperProxy(mapperInterface, session, methodCache);
        return newProxy(proxy);
    }

    protected I newProxy(MapperProxy<I> proxy) {
        return (I) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class<?>[]{mapperInterface}, proxy);
    }
}
